package com.bdsy.business.controller;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.bdsy.business.service.PriceTrendService;
import com.bdsy.common.annotation.Json;
import com.bdsy.system.annotation.SysLog;

@Controller
@RequestMapping(value = {"${web.adminPath}", "${web.appPath}"})
public class PriceTrendController {

	@Autowired
	PriceTrendService priceTrendService;
	
	
	@RequestMapping(value = "priceTrend")
	public String priceTrend() {
		return "admin/business/priceTrend";
	}
	
	@RequestMapping(value = "rateOfReturn")
	public String rateOfReturn() {
		return "admin/business/rateOfReturn";
	}
	
	
	
	
	/**
	 * 根据类别查询价钱趋势
	 */
	@Json
	@RequestMapping(value = "selectPriceTrend")
	public Object selectPriceTrend(int shopTypeId) {
		Map<String,Object> map = new LinkedHashMap<>();
		List yearList = new ArrayList<>();
		List priceList = new ArrayList<>();
		@SuppressWarnings("rawtypes")
		List<Map> priceTrendList = priceTrendService.selectPriceTrend(shopTypeId);
		for (int i = 0; i < priceTrendList.size(); i++) {
			yearList.add(priceTrendList.get(i).get("year"));
			priceList.add(priceTrendList.get(i).get("price"));
		}
		map.put("code", 200);
		map.put("msg", "success");
		map.put("year", yearList);
		map.put("price", priceList);
		return map;
	}
	
	
	/**
	 * 根据类别查询趋势
	 */
	@Json
	@RequestMapping(value = "sPriceTrend")
	public Object sPriceTrend(int shopTypeId) {
		Map<String,Object> map = new LinkedHashMap<>();
		List<String> typeList = new ArrayList<>();
		List<String> priceList = new ArrayList<>();
		@SuppressWarnings("rawtypes")
		List<Map> priceTrendList = priceTrendService.selectPriceTrend(shopTypeId);
		for (int i = 0; i < priceTrendList.size(); i++) {
			typeList.add((String) priceTrendList.get(i).get("shop_type_name"));
			priceList.add((String) priceTrendList.get(i).get("price"));
		}
		Set set = new  HashSet(); 
		List typeName = new  ArrayList(); 
		for (String tp : typeList) {
			if(set.add(tp)){
				typeName.add(tp);
			}
		}
		map.put("code", 200);
		map.put("msg", "success");
		map.put("type", typeName);
		map.put("price", priceList);
		return map;
	}
	
	
	/**
	 * 查询所有价钱趋势
	 */
	@Json
	@RequestMapping(value = "priceTrendList")
	public Object priceTrendList() {
		Map<String,Object> map = new LinkedHashMap<>();
		List<Integer> typeIdList = new ArrayList<>();
		List<String> typeList = new ArrayList<>();
		List<String> yearList = new ArrayList<>();
		@SuppressWarnings("rawtypes")
		List<Map> priceTrendList = priceTrendService.priceTrendList();
		for (int i = 0; i < priceTrendList.size(); i++) {
			typeIdList.add((Integer) priceTrendList.get(i).get("shop_type_id"));
			typeList.add((String) priceTrendList.get(i).get("shop_type_name"));
			yearList.add((String) priceTrendList.get(i).get("year"));
		}
		Set set = new  HashSet(); 
		List typeId = new  ArrayList(); 
		List type = new  ArrayList(); 
		List year = new  ArrayList(); 
		for (Integer tI : typeIdList) {
			if(set.add(tI)){
				typeId.add(tI);
			}
		}
		for (String tp : typeList) {
			if(set.add(tp)){
				type.add(tp);
			}
		}
		for (String ye : yearList) {
			if(set.add(ye)){
				year.add(ye);
			}
		}
		map.put("code", 200);
		map.put("msg", "success");
		map.put("typeId", typeId);
		map.put("type", type);
		map.put("year", year);
		return map;
	}
}
